package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    MenuService menuService;
    @RequestMapping("/getAllMenu")
    public Result getAllMenu(){
        List<Menu> menus=menuService.getAllMenu();
        return new Result(true, "",menus);
    }
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        try {
            menuService.add(menu);
            return new Result(true, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
       return new Result(false, "添加失败");
    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu){
        try {
            menuService.edit(menu);
            return new Result(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "修改失败");
    }
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            menuService.delete(id);
            return new Result(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "删除失败");
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Menu menu=menuService.findById(id);
            return new Result(true, "",menu);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "查询遇到错误");
    }
}
